<template>
	<view class="mainpadding">
		<view class="mainpadding">
			<view class="titletext hei_text bold flexcenter">实付金额</view>
			<view class="margin_top4 flexcenter">
				<text class="titletext hong_text bold">￥</text>
				<text class="titletext hong_text bold" style="font-size: 50rpx;">{{pay_price}}</text>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="titletext hei_text bold">选择支付方式</view>
			<view class="flexbetween pdl margin_top"  @click="pay_type=1">
				<view class="flexleft">
					<image src="../static/image/system/wxicon.png" class="paywayimg margin_right" mode=""></image>
					<view class="hei_text main_size bold">微信</view>
				</view>
				<view class="nocheckaudio" v-show="pay_type!=1"></view>
				<view class="checkaudio" v-show="pay_type==1"></view>
			</view>
			<view class="flexbetween margin_top" @click="pay_type=3">
				<view class="flexleft">
					<image src="../static/image/system/ye.png" class="paywayimg margin_right" mode=""></image>
					<view class="hei_text main_size bold">余额</view>
				</view>
				<view class="nocheckaudio" v-show="pay_type!=3"></view>
				<view class="checkaudio" v-show="pay_type==3"></view>
			</view>
		</view>
		<view class="posibottom mainpadding">
			<view class="submitbtn" @click="submit">立即支付</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				address_id: "",
				goods_num: "",
				goods_id: "",
				goods_sku_unique: "",
				remarks: "",
				platform: "wxMiniProgram",
				pay_price:"",
				pay_type:3,
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.address_id = data.address_id
			this.goods_num = data.goods_num
			this.goods_id = data.goods_id
			this.goods_sku_unique = data.goods_sku_unique
			this.remarks = data.remarks
			this.pay_price = data.pay_price
		},
		methods: {
			submit(){
				httpRequest.request("/api/goods_order/create_order", 'POST', {
					address_id:this.address_id,
					goods_num:this.goods_num,
					goods_id:this.goods_id,
					goods_sku_unique:this.goods_sku_unique,
					remarks:this.remarks,
					platform:this.platform,
					pay_type:this.pay_type,
					openid:uni.getStorageSync("openid")
				}, false, false, true).then(res => {
					if (res.code == 1) {
						if(this.pay_type==3){
							httpRequest.toast(res.msg)
							setTimeout(()=>{
								uni.navigateBack(1)
							},1000)
						}else{
							this.callPayMent(res.data.wxconfig)
							// httpRequest.toast("微信支付，后期需对接，建议在上个界面获取openid然后传递过来")
						}
					}else{
						httpRequest.toast(res.msg)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			callPayMent(wxpay){
				  uni.requestPayment({
				    provider: 'wxpay',
				  	timeStamp: wxpay.timeStamp,
				  	nonceStr: wxpay.nonceStr,
				  	package: wxpay.package,
				  	signType: wxpay.signType,
				  	paySign: wxpay.paySign,
				  	success: function (res) {
						// 支付成功
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
				  	}
				  });
			},
		}
	}
</script>

<style lang="scss" scoped>
	.paywayimg{
		width: 42rpx;
		height: 42rpx;
		min-width: 42rpx;
	}
</style>
